﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UM_SERVER.Timer
{
    /// <summary>
    ///     计时器管理器
    /// </summary>
    class TimeoutTimerManager: Manager
    {
        /// <summary>
        ///     向计时器集合中添加计时器
        /// </summary>
        /// <param name="timer" type="Hi_Baidu.TO_Timer">
        ///     <para>
        ///         要添加的计时器
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回true , 则表示添加成功
        /// </returns>
        public bool InstallTimer(TimeOutEvent timer)
        {
            if (!this.Equals(timer))
            {
                timer.CurrentTimer().Start();
                this.Collection.Add(timer);
                return true;
            }
            return false;
        }

        /// <summary>
        ///     从计时器集合中移除计时器
        /// </summary>
        /// <param name="timer" type="Hi_Baidu.TO_Timer">
        ///     <para>
        ///         要移除的计时器
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回true , 则表示移除成功
        /// </returns>
        public bool RemoveTimer(TimeOutEvent timer)
        {
            if (this.Equals(timer))
            {
                timer.CurrentTimer().Stop();
                this.Collection.Remove(timer);
                return true;
            }
            return false;
        }

        /// <summary>
        ///     从计时器集合中根据指定KEY移除计时器
        /// </summary>
        /// <param name="Key" type="string">
        ///     <para>
        ///         要移除的计时器KEY
        ///     </para>
        /// </param>
        /// <returns>
        ///     返回true , 则表示移除成功
        /// </returns>
        public bool RemoveTimer(String Key)
        {
            foreach (IMetaData member in this.Collection)
            {
                if (member.Key == Key)
                {
                    ((TimeOutEvent)member).CurrentTimer().Stop();
                    this.Collection.Remove(member);
                    return true;
                }
            }
            return false;
        }
    }
}
